---
title: 'Preferences for Trading Partners: Evidence from Citizens and Legislators in Latin America'
author: Andreas Dür, and Robert A. Huber
output:
  rmdformats::downcute:
    use_bookdown: true
    self_contained: true
    thumbnails: false
    lightbox: true
    gallery: false
    highlight: monochrome
  editor_options:
  chunk_output_type: console
---

```{r globopt, include = F}
knitr::opts_chunk$set(warning = FALSE)
```

This document provides the instructions for the replication data for the Chapter `Preferences for Trading Partners: Evidence from Citizens and Legislators in Latin America` in the Cambridge University Press book `Globalization in Latin America: The Law, Politics and Economics of Preferential Trade Agreements`

# How to proceed

All steps required to replicate the results in `R` can be run from the file `master_replication.R`. This file loads various individual scripts which pre-process the data, and runs the analyses. We cannot share large parts of the original data for copyright reasons but have outlined the sources including links in the subsequent scripts.

# Prepare Replication Data

After downloading our replication material from <https://doi.org/10.7910/DVN/5AMIA7>, please open the file `replication_material_CUP.Rproj`. You need to have installed `R` (version 4.2x) and `RStudio` for this. After opening the `.Rproj` file in `RStudio`, please open the file `master_replication.R` within the project. This script can be found in the subfolder `rcode`. In the next steps, we describe each of the steps of the `master_replication.R` file.

# Install/Load Packages

The following code installs all packages (if required). After that it loads the packages. The code then also uses the `here` package to set the working directory. Runtime of this code depends on the number of packages that you have already installed. However, even when you have no packages installed, it should only take a few minutes.

```{r pkgload}
run_start_total <- Sys.time()
run_start <- Sys.time()

pkgs <- c("here",
          "tidyverse",
          "car",
          "modelsummary",
          "countrycode",
          "plotrix",
          "lme4",
          "texreg")

# Function to check if packages are installed
# If not: package will be installed from CRAN and then loaded
# If: Package will be loaed

install_load <- function(packages){
  
  for (p in packages) {
    cat("Check package: '", p, "'...\n", sep = "")
    flush.console()
    
    if (p %in% rownames(installed.packages())) {
      
      cat("Package: '", p, "' is already installed...\n\n", sep = "")
      flush.console()
      
      library(p, character.only=TRUE)
      
    } else {
      
      cat("Package: '", p, "' is NOT installed! Will install now...\n\n", sep = "")
      install.packages(p)
      library(p,character.only = TRUE)
      
    }
  }
  cat("\nAll packages installed!\n\n")
}

# Apply function to all required packages

install_load(pkgs)

# Set wd with here() package

here::i_am("replication_material_CUP.Rproj")

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# Preparation of trade data for `Relative trade ties` Variable

The script `prep_trade_final.R` in the subfolder `rcode` pre-processes the trade data. We cannot share the underlying data. To allow replication, the `if` function circumvents in the `master_replication.R` file allows the replication material to function without this data being shared.

```{r, echo=T}
run_start <- Sys.time()
source("rcode/prep_trade_final.R")
#Note: We cannot share this data but have shared to code to make transparent how we process the data.

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time

```

# Preparation of trade data for `US Disgreement in UNGA voting` Variable

The script `prep_unga_final.R` in the subfolder `rcode` pre-processes the trade data. We cannot share the underlying data. To allow replication, the `if` function circumvents in the `master_replication.R` file allows the replication material to function without this data being shared.

```{r, echo=T}
run_start <- Sys.time()
source("rcode/prep_unga_final.R")
#Note: We cannot share this data but have shared to code to make transparent how we process the data.

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time

```

# Analysis of Latinobarometro data

The script `latinobarometro_final.R` in the subfolder `rcode` runs all analyses on the latinobarometro data. We cannot share the underlying raw data but share both the code from raw data to final data, as well as the final data. This file produces table 2 and figures 1 to 3. 

```{r, echo=T}
run_start <- Sys.time()
source("rcode/latinobarometro_final.R")

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time

```

# Analysis of Latinobarometro data

The script `pela_final.R` in the subfolder `rcode` runs all analyses on the PELA data. We cannot share the underlying raw data but share both the code from raw data to final data, as well as the final data. This file produces table 3 and figure 4. 

```{r, echo=T}
run_start <- Sys.time()
source("rcode/pela_final.R")

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time

```


# Session Info

This notebook was run using the following setup:

```{r}
pander::pander(sessionInfo())

run_stop_total <- Sys.time()
run_time_total <- (run_stop_total - run_start_total)
run_time_total
```
